package com.huabo.utils;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Name;
import com.alibaba.druid.util.JdbcConstants;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Jackie Liu on 2017/6/28.
 */
public class Test {

    static String middle_ftpServer = "192.168.11.251";
    static String middle_user = "cs";
    static String middle_password = "123";
    static String middle_dir = "/image/NWKPHOTO/Middle/2009/3";

    public static void main(String[] args) {
       /* FTPClientConfigure configure = new FTPClientConfigure();
        configure.setHost("192.168.11.110");
        configure.setUserName("cs");
        configure.setPassword("123");
        configure.setClientTimeout(120000);

        FtpFileOperation operation = new FtpFileOperation();
        operation.setConfigure(configure);
        operation.setSaveFilePath("/image");
        operation.afterPropertiesSet();
        *//*try {
            operation.save(new File("E:\\jaas\\jaas.config"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }*//*
        //operation.read("/image/aa.config");
        byte[] result = operation.read("/image/aa.txt");
        System.out.println(result.length);
        operation.close();
        System.out.println("aaaaaaaaaa");*/

        String sql= ""
                + "insert into tar select * from boss_table bo, ("
                + "select a.f1, ff from emp_table a "
                + "inner join log_table b "
                + "on a.f2 = b.f3"
                + ") f "
                + "where bo.f4 = f.f5 "
                + "group by bo.f6 , f.f7 having count(bo.f8) > 0 "
                + "order by bo.f9, f.f10;"
                + "select func(f) from test1; "
                + "";
        String dbType = JdbcConstants.POSTGRESQL;

        //格式化输出
        String result = SQLUtils.format(sql, dbType);
        System.out.println(result); // 缺省大写格式
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);

        //解析出的独立语句的个数
        System.out.println("size is:" + stmtList.size());
        for (int i = 0; i < stmtList.size(); i++) {

            SQLStatement stmt = stmtList.get(i);

            PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
            stmt.accept(visitor);
            Map<String, String> aliasmap = visitor.getAliasMap();
            for (Iterator iterator = aliasmap.keySet().iterator(); iterator.hasNext(); ) {
                String key = iterator.next().toString();
                System.out.println("[ALIAS]" + key + " - " + aliasmap.get(key));
            }
            Set<Column> groupby_col = visitor.getGroupByColumns();
            //
            for (Iterator iterator = groupby_col.iterator(); iterator.hasNext(); ) {
                Column column = (Column) iterator.next();
                System.out.println("[GROUP]" + column.toString());
            }
            //获取表名称
            System.out.println("table names:");
            Map<Name, TableStat> tabmap = visitor.getTables();
            for (Iterator iterator = tabmap.keySet().iterator(); iterator.hasNext(); ) {
                Name name = (Name) iterator.next();
                System.out.println(name.toString() + " - " + tabmap.get(name).toString());
            }
            //System.out.println("Tables : " + visitor.getCurrentTable());
            //获取操作方法名称,依赖于表名称
            System.out.println("Manipulation : " + visitor.getTables());
            //获取字段名称
            System.out.println("fields : " + visitor.getColumns());
        }
    }
}
